package com.jcl.recruit.api.user.controller;

import com.jcl.recruit.user.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author 刘明新
 * @date 2018/8/2 下午7:12
 */
@RestController
@RequestMapping("/api/user")
public class UserLoginController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     * @param result
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public Map<String,Object> login(@RequestBody Map<String,String> result){
        Map<String,Object> map = new LinkedHashMap<>();
        String name = result.get("username");
        String password = result.get("password");
        if(StringUtils.isBlank(name) || StringUtils.isBlank(password)){
            map.put("type","error");
            map.put("message","没有这个用户");
            map.put("errors","");
            return map;
        }
        Map<String,Object> user = userService.selectPrimaryKey(name,password);
        if(user == null){
            map.put("type","error");
            map.put("message","密码不对");
            map.put("user","");
            return map;
        }

        user.put("password", null);
        map.put("type","success");
        map.put("message","成功");
        map.put("user",user);
        return map;
    }

    /**
     * 通过token登录
     * @param result
     * @return
     */
    @RequestMapping(value = "login_token",method = RequestMethod.POST)
    public Map<String,Object> loginToken(@RequestBody Map<String,String> result){
        Map<String,Object> map = new LinkedHashMap<>();
        String token = result.get("token");
        if(StringUtils.isBlank(token)){
            map.put("type","error");
            map.put("message","没有这个用户");
            map.put("errors","");
            return map;
        }
        Map<String,Object> user = userService.selectPrimaryKeyByToken(token);
        if(user == null){
            map.put("type","error");
            map.put("message","密码不对");
            map.put("user","");
        }
        map.put("type","success");
        map.put("message","成功");
        map.put("user",user);
        return map;
    }

}
